package com.example.coursemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.mapper.CampusCommentsMapper;
import com.example.coursemanagement.mapper.CampusMapper;
import com.example.coursemanagement.mapper.CatalogueMapper;
import com.example.coursemanagement.mapper.UserMapper;
import com.example.coursemanagement.pojo.Campus;
import com.example.coursemanagement.pojo.Campus_comments;
import com.example.coursemanagement.pojo.Catalogue;
import com.example.coursemanagement.pojo.User;
import com.example.coursemanagement.service.ICampusCommentsService;
import com.example.coursemanagement.service.ICampusService;
import com.example.coursemanagement.service.ICatalogueService;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 这是目录表格，通过这个查寻课程的各个章节链接 服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-10-27
 */
@Service
public class CampusService extends ServiceImpl<CampusMapper, Campus> implements ICampusService {


    @Autowired
    private CampusMapper campusMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CampusCommentsMapper commentsMapper;

    @Autowired
    private ICampusCommentsService commentsService;

    @Override
    public CommonResult<Object> publishCampus(Campus campus) {
        if(Objects.isNull(campus.getContext()))
        {
            return CommonResult.failed("发布内容为空");
        }
        User user = userMapper.selectById(campus.getUserId());
        if(Objects.isNull(user)){
            return CommonResult.failed("用户不存在");
        }
        if(campusMapper.insert(campus)>0){
            return CommonResult.success(campus,"发布成功");
        }
        return CommonResult.failed("发布失败");
    }

    @Override
    public CommonResult<Object> selectAllCampus() {
        List<Campus> campusList = campusMapper.selectList(null);
        return CommonResult.success(campusList,"查询成功，共有"+campusList.size()+"条");
    }

    @Override
    public CommonResult<Object> updateCampus(Campus campus) {
        if(campusMapper.updateById(campus)>0){
            return CommonResult.success(campus,"修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @Override
    public CommonResult<Object> selectByCampusId(Integer campusId) {
        Campus campus = campusMapper.selectById(campusId);
        if(Objects.isNull(campus)){
            return CommonResult.failed("查询失败");
        }
        return CommonResult.success(campus,"查询成功");
    }

    @Override
    public CommonResult<Object> selectByUserId(Integer userId) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        List<Campus> campusList = campusMapper.selectByMap(map);
        return CommonResult.success(campusList,"查询成功，共有"+campusList.size()+"条");
    }

    @Override
    @Transactional
    public CommonResult<Object> deleteByCampusId(Integer campusId) {
        //todo 删除评论/批量删除
        List<Campus_comments> commentsList = (List<Campus_comments>) commentsService.selectCommentsByCampusId(campusId).getData();
        int deleteNum = commentsList.size();
        for(Campus_comments campus_comments:commentsList){
            if(commentsMapper.deleteById(campus_comments.getCommentId())>0){
                deleteNum--;
            }
        }
        if(campusMapper.deleteById(campusId)>0){
            if(deleteNum==0)
                return CommonResult.success(null,"删除成功");
            else{
                return CommonResult.success(null,"资讯删除成功，但是评论未删除完全");
            }
        }
            return CommonResult.failed("删除失败");
    }
}
